package com.erp.module.jiehao

import android.app.Activity
import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.util.Log
import android.view.View
import android.view.inputmethod.EditorInfo
import com.erp.Myapp
import com.erp.R
import com.erp.bean.JieHaoKBean
import com.erp.mclass.QuanXian
import com.erp.module.jiehao.scJieHao.ScJieHaoKActivity
import com.erp.myAdapter.ZhiXiangKAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragment_jiehao.*
import java.util.*

/**
 * Created by Administrator on 2017/11/16/016.
 * 作者：刘洋
 * 纸箱分配
 */
class FragmentKZhiXiang : BaseKFragment(),  JieHaoContract.JieHaoView, View.OnClickListener, PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener {


    companion object {
        val FENPEITIME="ZHIXIANGTIME"
    }
    lateinit var P : IJieHaoP
    lateinit var adapter : ZhiXiangKAdapter
    lateinit var jiehaolist :MutableList<JieHaoKBean>
    var updialog: AlertDialog? = null
    private val f = "FragmentKZhiXiang"
    override fun getLayoutId(): Int {
        fragmentname=f
        return R.layout.fragment_jiehao
    }

    override fun initData() {
        jiehaolist=ArrayList()
        P= IJieHaoP(2,jiehaolist,this, activity!!)
        adapter= ZhiXiangKAdapter(jiehaolist, activity!!, P, QuanXian.isFenPeiSuoYouZhiXiang, QuanXian.isQuXiaoFenPeiZhiXiang)
        JiJianLv.adapter=adapter
        JiJianFragmentPv.setheader(true)
        JiJianFragmentPv.setOnHeaderRefreshListener(this)
        JiJianFragmentPv.setOnFooterRefreshListener(this)
        ScanLl.setOnClickListener(this)
        SouSuoIv.setOnClickListener(this)
        SouSuoEt.hint="输入纸箱"
        ShuangChuanTv.text="上传纸箱"
        SouSuoEt.visibility=View.VISIBLE
        P.gainData("ZhiXiang")
        JiJianFragmentPv.headerRefreshing()
        SouSuoEt.setOnEditorActionListener { v, actionId, event ->
            var isOK = true
            when (actionId) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    P.sousuo(SouSuoEt.text.toString())
                    updialog = Myutil.showupdatedialog(activity, "正在搜索纸箱数据", false)
                    Myutil.notjianpan(activity,SouSuoIv)
                }
                else -> isOK = false
            }
            isOK
        }
    }

    fun qingkong(){
        if(isFirstLoad){
            return
        }
        if(isFirstLoad){
            return
        }
        JiJianFragmentPv.headerRefreshing()
    }

    override fun fenPeiJieHaoOk(data: String) {
        P.xiala()
        if(updialog!=null) {
            updialog?.dismiss()
        }
    }

    override fun fenPeiJieHaoNo(erroe: String) {
        if(updialog!=null) {
            updialog?.dismiss()
        }
        Myutil.MyTost(Myutil.decodeUnicode(erroe))
        Log.d("TAAAG","erroe="+erroe)
    }
    override fun onClick(v: View?) {
        when(v){
            ScanLl -> {
                if(QuanXian.isBianJiZhiXiang) {
                    val intent = Intent(activity, ScJieHaoKActivity::class.java)
                    intent.putExtra("code",2)
                    startActivityForResult(intent, 1)
                }else{
                    Myutil.MyTost("你没有上传纸箱的权限")
                }
            }
            SouSuoIv -> {
                P.sousuo(SouSuoEt.text.toString())
                updialog = Myutil.showupdatedialog(activity, "正在搜索纸箱数据", false)
                Myutil.notjianpan(activity,SouSuoIv)
            }
        }
    }
    override fun fenpeijiehao() {
        updialog = Myutil.showupdatedialog(activity, "正在访问服务器", false)
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null || resultCode!= Activity.RESULT_OK){
            return
        }
        when(requestCode){
            1 -> {
                if(data.getBooleanExtra("isadd",false)){
                    P.xiala()
                }
            }
        }
    }

    override fun xialaOk() {
        updialog?.dismiss()
        JiJianFragmentPv?.onHeaderRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun xialaNo() {
        updialog?.dismiss()
        JiJianFragmentPv?.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        updialog?.dismiss()
        JiJianFragmentPv?.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun shanglaNo() {
        updialog?.dismiss()
        JiJianFragmentPv?.onFooterRefreshComplete()
    }
    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }

    override fun onFooterRefresh(view: PullToRefreshView) {
        P.shangla()
    }
    override fun onHeaderRefresh(view: PullToRefreshView) {
        P.xiala()
    }
    override fun PutTime() {
        Myapp.editor.putLong(FENPEITIME, Date().time / 1000)
        Myapp.editor.commit()
    }
    override fun deleteOk(postion: Int) {
    }

    override fun deleteNo(error: String) {
    }

}